class_name PlayerBlackBoard  
extends StatusBlackBoard

var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
enum Direction {
	LEFT = -1,
	RIGHT = 1,
}

const SPEED = 160.0
const JUMP_VELOCITY = -320.0
const WALL_SLIDE_SPEED = 20.0

@export var max_speed: float = 180
@export var acceleration :float = 2000
@onready var stats: Stats = $Stats

var player:Player

const SLIDING_ENERGY = 4.0

# 受伤信息
var pending_damage:Damage

func create_damage(source:Node2D, hurt_amount:int, type:int =1 ) -> Damage:
	pending_damage = Damage.new()
	pending_damage.source = source
	pending_damage.amount = hurt_amount
	pending_damage.type = type
	return pending_damage
	
func get_damage() -> Damage:
	return pending_damage
	
func has_damage() -> bool:
	return pending_damage != null
	
func free_damage() ->void:
	pending_damage = null
	
# 状态栏
func get_stats() -> Stats:
	return stats
	
func update_health(v:int)->int:
	stats.health -= v
	return stats.health
	
func get_health()->int:
	return stats.health

func get_energy() -> float:
	return stats.energy
	
func has_enough_energy() -> bool:
	return stats.energy >= SLIDING_ENERGY
	
func update_energy()->float:
	stats.energy -= SLIDING_ENERGY
	return stats.energy

# 这里是内存存储，切换场景的时候保持状态
func to_dict() -> Dictionary:
	var stats = get_stats().to_dict()
	var x = player.position.x
	var y = player.position.y
	return {
		stats = stats,
		direction = player.get_face_direction(),
		position = {
			x = x, 
			y = y, 
		}
	}
	
func from_dict(dict:Dictionary) -> void:
	if dict.is_empty():
		return 
	if dict.has("stats"):
		stats.from_dict(dict.get("stats"))

func set_actor(v:Player):
	player = v
func get_actor() -> Player:
	return player
